﻿using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Transactions;
using System;

namespace ScrumFactory.Services.Logic {

    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
    [Export(typeof(ITeamService))]
    public class TeamService : ITeamService {


        private Data.ITeamRepository teamRepository;

        [ImportingConstructor()]
        public TeamService([Import] Data.ITeamRepository teamRepository) {
            this.teamRepository = teamRepository;
        }

        [WebGet(UriTemplate = "Members/{memberUId}", ResponseFormat = WebMessageFormat.Json)]
        public MemberProfile GetMember(string memberUId) {
            return teamRepository.GetMember(memberUId);
        }

        [WebGet(UriTemplate = "Members", ResponseFormat = WebMessageFormat.Json)]
        public ICollection<MemberProfile> GetAllMembers() {
            return teamRepository.GetAllMembers();
        }

        [WebGet(UriTemplate = "Members/?projectUId={projectUId}", ResponseFormat = WebMessageFormat.Json)]
        public ICollection<MemberProfile> GetProjectMembers(string projectUId) {
            return teamRepository.GetProjectMembers(projectUId);
        }

        [WebInvoke(Method="POST", UriTemplate = "Members/{memberUId}/Memberships", ResponseFormat = WebMessageFormat.Json)]
        public void AddProjectMembership(string memberUId, ProjectMembership membership) {

            if (!CheckMemberWorkLoad(membership))
                throw new WebFaultException<String>("BRE_INVALID_MEMBER_WORKLOAD", System.Net.HttpStatusCode.BadRequest);
                
            teamRepository.SaveProjectMembership(membership);
        }

        [WebInvoke(Method = "PUT", UriTemplate = "Members/{memberUId}/Memberships", ResponseFormat = WebMessageFormat.Json)]
        public void UpdateProjectMembership(string memberUId, ProjectMembership membership) {

            if (!CheckMemberWorkLoad(membership))
                throw new WebFaultException<String>("BRE_INVALID_MEMBER_WORKLOAD", System.Net.HttpStatusCode.BadRequest);

            teamRepository.SaveProjectMembership(membership);
        }

        [WebInvoke(Method = "DELETE", UriTemplate = "Members/{memberUId}/Memberships", ResponseFormat = WebMessageFormat.Json)]
        public void RemoveProjectMembership(string memberUId, ProjectMembership membership) {
            teamRepository.DeleteProjectMembership(membership);
        }


        private bool CheckMemberWorkLoad(ProjectMembership membership) {

            if (membership.DayAllocation == null)
                return true;

            if(membership.DayAllocation>4)
                return false;

            MemberProfile member = GetMember(membership.MemberUId);

            if(member==null)
                return true;

            ProjectMembership ms = member.Memberships.SingleOrDefault(m => m.IsSame(membership));
            int msOldOcupation = 0;
            if (ms != null && ms.DayAllocation!=null)
                msOldOcupation = (int)ms.DayAllocation;

            return (member.DayOccupation - msOldOcupation + membership.DayAllocation) <= 4;
        }

        
    }
}
